package com.hrc.springbootdemo.mq.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class OriginDemo {

    /**
     * 主机
     */
    private final String host = "127.0.0.1";
    /**
     * 端口
     */
    private int port = 5672;
    /**
     * 虚拟主机
     */
    private String virtualHost = "/";
    /**
     * 用户名
     */
    private String username = "guest";
    /**
     * 密码
     */
    private String password = "guest";
    /**
     * 交换机
     */
    private String exchange = "exchange";
    /**
     * 队列
     */
    private String queue = "queue";
    /*路由键*/
    private String routingKey = "routingKey";
    private String message = "Hello World!";

    public static void main(String[] args) throws IOException, TimeoutException {
        OriginDemo originDemo = new OriginDemo();
        int n = 2;
        while  (n > 0) {
            n--;
            originDemo.producer();
        }
        originDemo.consumer();
    }



    /**
     * 消费者
     */
    public void consumer() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            System.out.println(" [x] Received '" + message + "'");
        };
        CancelCallback cancelCallback = (consumerTag) -> {
            //主动取消不会触发
            System.out.println("消息被中断");
        };
        channel.basicConsume(queue,true,deliverCallback,cancelCallback);
    }

    /**
     * 生产者
     */
    public void producer() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(exchange, BuiltinExchangeType.DIRECT, true);
        channel.queueDeclare(queue, true, false, false, null);
        channel.queueBind(queue, exchange, routingKey);
        channel.basicPublish(exchange, routingKey, null, message.getBytes());
        channel.close();
        connection.close();
    }
}
